home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / code_lib / objlibr / objlib12 / sample2 / listwin2.bas < prev    next >
Encoding:
BASIC Source File  |  1995-06-05  |  8.7 KB  |  280 lines

  1. Option Explicit
  2. '
  3. 'prevent needless paints
  4. Dim resizing%
  5.  
  6. 'global constants for list boxes
  7. Global Const LISTTEXTLEFT = 44
  8. Global Const LISTITEMHEIGHT = 36
  9. 'types for Progman windows================
  10.  
  11. 'constant size data for all PMwindows
  12. 'for this sample, all common values are placed in a seperate structure
  13. 'to reduce duplication of data
  14. Type COMMONDATA
  15.     cell As PointAPI        'w,h of normal cell
  16.     pic As PointAPI         'x,y offset of cell image
  17.     cap As rect             'x,y offset,r,b offset of caption
  18.     'control panel colors
  19.     bkg As Long             'window background color
  20.     txt As Long             'window text
  21.     hilite As Long          '
  22.     hilitetext As Long      '
  23. End Type
  24. Global cdata As COMMONDATA
  25.  
  26. 'variable data for each window - each instance of the list is created
  27. 'by declaring a listdata structure
  28. Type LISTDATA
  29.     toprow As Integer           'client area's top
  30.     itemcount As Integer        'total items
  31.     active As Integer           'active item
  32.     cols As Integer
  33.     rows As Integer
  34.     visrows As Integer
  35.     width As Integer
  36. End Type
  37.  
  38.  
  39.  
  40. 'used to transfer data between windows
  41. Global gItem As ITEMDATA
  42.  
  43. 'API constants and types====================
  44. Global Const black = &H0
  45. Global Const white = &HFFFFFF
  46. Global Const lgrey = &HC0C0C0
  47. Global Const PATPAINT = &HFB0A09
  48. Global Const PATCOPY = &HF00021
  49. Global Const SRCCOPY = &HCC0020
  50. Global Const GWW_HINSTANCE = (-6)
  51. Global Const WM_USER = &H400
  52. Global Const GWL_STYLE = (-16)
  53. 'draw text
  54. Global Const DT_CALCRECT = &H400
  55. Global Const DT_CENTER = &H1
  56. Global Const DT_NOPREFIX = &H800
  57. Global Const DT_VCENTER = &H4
  58. Global Const DT_WORDBREAK = &H10
  59. Global Const DT_INTERNAL = &H1000
  60. Global Const DT_SINGLELINE = &H20
  61. Global Const DT_LEFT = &H0
  62. Global Const DT_GETRECT = DT_CALCRECT Or DT_NOPREFIX Or DT_CENTER Or DT_WORDBREAK
  63. Global Const DT_ICONCAP = DT_NOPREFIX Or DT_WORDBREAK Or DT_CENTER
  64. Global Const DT_LISTCAP = DT_NOPREFIX Or DT_LEFT  ' Or DT_WORDBREAK Or DT_SINGLELINE
  65. Global Const DT_ICONTITLE = DT_NOPREFIX Or DT_CENTER Or DT_WORDBREAK 'Or DT_VCENTER
  66.  
  67. Declare Function bitblt% Lib "GDI" (ByVal hDestDC%, ByVal x%, ByVal y%, ByVal nWidth%, ByVal nHeight%, ByVal hSrcDC%, ByVal XSrc%, ByVal YSrc%, ByVal dwRop&)
  68. Declare Function CreateDC Lib "GDI" (ByVal lpDriverName As String, ByVal lpDeviceName As Any, ByVal lpOutput As Any, ByVal lpInitData As Any) As Integer
  69. Declare Function CreateSolidBrush% Lib "GDI" (ByVal crColor&)
  70. Declare Function DeleteDC Lib "GDI" (ByVal hDC As Integer) As Integer
  71. Declare Function DeleteObject% Lib "GDI" (ByVal hObject%)
  72. Declare Function DrawText% Lib "User" (ByVal hDC%, ByVal lpStr$, ByVal nCount%, lpRect As rect, ByVal wFormat%)
  73. Declare Function DrawIcon Lib "USER" (ByVal lpHandle As Integer, ByVal xcoord As Integer, ByVal ycoord As Integer, ByVal hicon As Integer) As Integer
  74. Declare Function ExtractIcon Lib "shell" (ByVal lpHandle As Integer, ByVal lpExe As String, ByVal lpiconindex As Integer) As Integer
  75. Declare Function GetSysColor& Lib "User" (ByVal nIndex%)
  76. Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer, ByVal nIndex As Integer) As Integer
  77. Declare Function SetTextColor& Lib "GDI" (ByVal hDC%, ByVal crColor&)
  78. Declare Function PatBlt% Lib "GDI" (ByVal hDC%, ByVal x%, ByVal y%, ByVal nWidth%, ByVal nHeight%, ByVal dwRop&)
  79. Declare Function SelectObject% Lib "GDI" (ByVal hDC%, ByVal hObject%)
  80.  
  81. Sub InitList (F As Form, ld As LISTDATA)
  82. Dim inst%, i%, s$
  83. F.BackColor = cdata.bkg
  84. F.ForeColor = cdata.txt
  85. ld.toprow = 0
  86. ld.active = 1
  87. End Sub
  88.  
  89. Sub ItemClick (F As Form, ld As LISTDATA, id() As ITEMDATA, y)
  90. Dim n%, old%
  91. Dim textr  As rect, cr As rect
  92.  
  93. '===set focus to clicked item=====================
  94. y = (y) \ LISTITEMHEIGHT:  'Debug.Print x, y
  95. 'determine relative item #
  96. n = y + 1'Debug.Print n
  97. 'determine absolute item #
  98. n = n + ld.toprow'Debug.Print n
  99. 'set active item
  100. If n <= ld.itemcount Then
  101.     'old is a 1-based index; the draw routine uses a 0-base
  102.     old% = ld.active - 1
  103.     ld.active = n
  104. End If
  105.  
  106. 'erase old hilite
  107. textr.left = LISTTEXTLEFT
  108. textr.right = ld.width - textr.left
  109. cr.left = LISTTEXTLEFT - 8
  110. cr.right = ld.width
  111.  
  112. 'valid index?
  113. If old >= 0 And old < ld.itemcount Then
  114.     'is it still visible?
  115.     n = old - ld.toprow
  116.     If n >= 0 And n < ld.visrows Then
  117.         
  118.         'size of caption rect:
  119.         textr.top = n * LISTITEMHEIGHT + 8
  120.         textr.bottom = (n + 1) * LISTITEMHEIGHT
  121.         '
  122.         'size of hilite rect
  123.         cr.top = textr.top - 8
  124.         cr.bottom = cr.top + LISTITEMHEIGHT
  125.         PaintHilite F, 0, id(old + 1).cap, textr, cr
  126.     End If
  127. End If
  128.  
  129. 'draw new hilite
  130. n = ld.active - 1 - ld.toprow:  'Debug.Print "rel" & n
  131.     'check if its visible:'Debug.Print "total" & ld.visrows * ld.cols
  132.     If n < 0 Or n > ld.visrows - 1 Then Exit Sub
  133.     
  134.     'size of caption rect:
  135.     textr.top = n * LISTITEMHEIGHT + 8
  136.     textr.bottom = textr.top + 24: 'Debug.Print cr.left, cr.top, cr.right, cr.bottom
  137.     cr.top = textr.top - 8
  138.     cr.bottom = cr.top + LISTITEMHEIGHT
  139.     PaintHilite F, -1, id(ld.active).cap, textr, cr
  140.  
  141. End Sub
  142.  
  143. Sub LoadIcons (F As Form, ld As LISTDATA, id() As ITEMDATA)
  144. Dim inst%, i%, r%
  145.     mnu.loader.Picture = LoadPicture()
  146.     F.pics.Cls
  147.     inst% = GetWindowWord(F.hWnd, GWW_HINSTANCE)
  148.     'extract the icon for each item and put them all into
  149.     'a single bitmap
  150.     F.pics.Move 0, 0, ld.itemcount * 32, 32
  151.     For i% = 1 To ld.itemcount
  152.         GetIcon id(i).iconpath, id(i).iconindex
  153.         r = bitblt(F.pics.hDC, (i - 1) * 32, 0, 32, 32, mnu.loader.hDC, 0, 0, SRCCOPY)
  154.     Next
  155. End Sub
  156.  
  157. Sub PaintHilite (F As Form, op%, s$, tr As rect, cr As rect)
  158. Dim bkgcolor&, txtcolor&, r%
  159. Dim offset%'offset of icon caption
  160. Dim hbrOld%, hbr%, cOld& 'api stuff
  161. '
  162. 'n = 0 erase hilite; n = -1 paint hilite
  163. If op Then
  164.     bkgcolor& = cdata.hilite
  165.     txtcolor& = cdata.hilitetext
  166. Else
  167.     bkgcolor& = cdata.bkg
  168.     txtcolor = cdata.txt
  169. End If
  170.         'paint a hilite rectangle:
  171.         hbr = CreateSolidBrush(bkgcolor&)
  172.         hbrOld = SelectObject(F.hDC, hbr)
  173.         r = PatBlt(F.hDC, cr.left, cr.top, cr.right - cr.left, cr.bottom - cr.top, PATCOPY)
  174.         F.Line (0, cr.top)-(36, cr.top + 35), bkgcolor&, B
  175.         'paint hilite text:
  176.         cOld = SetTextColor(F.hDC, txtcolor&)
  177.         r = DrawText(F.hDC, s, Len(s), tr, DT_LISTCAP)
  178.         'cleanup
  179.         cOld = SetTextColor(F.hDC, cOld)
  180.         hbr = SelectObject(F.hDC, hbrOld)
  181.         r = DeleteObject(hbr)
  182. End Sub
  183.  
  184. Sub PaintList (F As Form, ld As LISTDATA, id() As ITEMDATA)
  185. Dim i%, r%
  186. Dim y% 'y pos to draw icon
  187. Dim ypos% 'y pos of item
  188. Dim pstart%, pend% 'indexes of first and last visible icons
  189. Dim cr As rect, tr  As rect 'for drawing text
  190.  
  191. 'calculate which icons to show:
  192. pstart% = ld.toprow + 1': Debug.Print pstart
  193. pend% = pstart% + ld.visrows - 1
  194. If pend% > ld.itemcount Then pend% = ld.itemcount': Debug.Print pend
  195. '
  196. 'draw the icons:
  197. y = -LISTITEMHEIGHT + 2
  198. For i = pstart% To pend%
  199.     y = y + LISTITEMHEIGHT'(new row)
  200.     r = bitblt(F.hDC, 2, y, 32, 32, F.pics.hDC, (i - 1) * 32, 0, SRCCOPY)
  201. Next
  202.  
  203. y = -LISTITEMHEIGHT
  204. tr.left = LISTTEXTLEFT
  205. tr.right = ld.width' - tr.left
  206. For i = pstart% To pend%
  207.     y = y + LISTITEMHEIGHT'(new row)
  208.     'define the rect to draw text in:
  209.     tr.top = y + 8
  210.     tr.bottom = y + LISTITEMHEIGHT
  211.     '
  212.     If i = ld.active Then
  213.         cr.left = tr.left - 8
  214.         cr.top = y
  215.         cr.bottom = y + LISTITEMHEIGHT
  216.         cr.right = F.ScaleWidth
  217.         PaintHilite F, -1, id(i).cap, tr, cr
  218.     Else
  219.         r = DrawText(F.hDC, id(i).cap, Len(id(i).cap), tr, DT_LISTCAP)
  220.     End If
  221. Next
  222. Exit Sub
  223. '
  224. paintlisterr:
  225. MsgBox "Err: " & Err & nl & Error(Err), , "UNABLE TO PAINT WINDOW"
  226. Exit Sub
  227.  
  228. End Sub
  229.  
  230. Sub ResizeList (F As Form, ld As LISTDATA)
  231. 'Dim x%, y%
  232. 'Dim r As rect
  233. Debug.Print "Resizing"
  234. resizing = -1
  235. '
  236. ld.rows = ld.itemcount
  237. If ld.rows < 1 Then ld.rows = 1
  238. ld.cols = 1
  239. ld.visrows = F.ScaleHeight \ LISTITEMHEIGHT + 1: Debug.Print ld.rows, ld.visrows
  240.  
  241. F.vs.Visible = 0
  242. '
  243. If ld.rows > ld.visrows Then
  244.     F.vs.Move F.ScaleWidth - F.vs.Width, 0, F.vs.Width, F.ScaleHeight
  245.     F.vs.Visible = -1
  246.     F.vs.Max = ld.rows - ld.visrows
  247. Else
  248.     ld.toprow = 0
  249.     F.vs.Visible = 0
  250. End If
  251. ld.width = F.ScaleWidth
  252. '
  253. resizing = 0
  254.  
  255. End Sub
  256.  
  257. Sub SetColor ()
  258. cdata.bkg = GetSysColor(5)
  259. cdata.txt = GetSysColor(8)
  260. cdata.hilite = GetSysColor(13)
  261. cdata.hilitetext = GetSysColor(14)
  262. End Sub
  263.  
  264. Sub SetScaleData ()
  265. Dim i%, l&
  266. tx = screen.TwipsPerPixelX
  267. ty = screen.TwipsPerPixelY
  268. 'set constants for all 'window' forms
  269. cdata.cell.x = 100
  270. cdata.cell.y = 80
  271. cdata.pic.x = 32
  272. cdata.pic.y = 8
  273. cdata.cap.left = 2
  274. cdata.cap.top = 40
  275. cdata.cap.right = cdata.cell.x - 2 * cdata.cap.left
  276. cdata.cap.bottom = cdata.cell.y - cdata.cap.top
  277. '
  278. End Sub
  279.  
  280.